import json
import os

import django
import requests
from django.db.utils import IntegrityError
from lxml import etree

from Spider.close20 import get_last20mean
from Spider.config import CONFIG

os.environ.setdefault('DJANGO_SETTINGS_MODULE',
                      'Stack.settings')  # 其中B是写你的django项目名，如：TestFramework.settings
django.setup()
from WebStack.serializers import StackSerializer, FundSerializer

from multiprocessing.pool import ThreadPool


def get_stcak_codes():
    """
    1天刷新1次

    :return:
    """
    response = requests.get("http://www.aigaogao.com/tools/compare_a_h.html")
    html = etree.HTML(response.text)
    stack_codes = html.xpath('//*[@id="tbldata"]//tr/td//span/@aggsymbol')  # 股票代码
    s_names = html.xpath('//*[@id="tbldata"]//*[contains(@class,"cell")]/text()')[1:]
    s_hcodes = stack_codes[::6]
    s_acodes = stack_codes[3::6]
    seri = StackSerializer()
    for i, s_name in enumerate(s_names):
        stack = {
            's_id': s_acodes[i],
            's_name': s_name,
            's_hcode': s_hcodes[i],
            's_acode': s_acodes[i]
        }
        try:
            seri.create(stack)
        except IntegrityError as e:
            pass


from Spider.tool import cal_run_time


@cal_run_time(cal_func_place="更新a股前10天平均值")
def update_stack_close20():
    """
    更新a股前10天的平均值

    :return:
    """

    stacks = requests.get("{}/index/api/stack/?format=json".format(CONFIG.LOCAL_URL)).text
    stacks = json.loads(stacks)
    task_pool = ThreadPool(60)
    s_mean20s = task_pool.map(get_last20mean, stacks)
    for s_mean20, stack in zip(s_mean20s, stacks):
        res = requests.patch("{}/index/api/stack/{}/".format(CONFIG.LOCAL_URL, stack["s_acode"]),
                             data={"s_20mean": s_mean20})


def get_fund_codes():
    """
    聚宽投资网获取 LOF列表数据

    :return:
    """
    url = "https://www.joinquant.com/help/api/getContent?name=fund&token=d52a073acba21ab6450d25bfd218ababa56b7690ef2bbeba830e9227f5b1ff5ff7fbff96767dbf4433da108860323b03"
    response = requests.get(url)
    data = json.loads(response.text)['data']
    seri = FundSerializer()
    if data:
        html = etree.HTML(data)
        fund_codes = html.xpath('//h4[@id="LOF列表"]/following-sibling::*[1]//tbody//td/text()')
        row_codes = fund_codes[0::4]
        f_names = fund_codes[1::4]
        f_fundtypes = fund_codes[3::4]

        for i, row_code in enumerate(row_codes):
            fund = {
                "f_id": row_codes[i][:6],
                "f_name": f_names[i],
                "f_code": row_codes[i][:6],
                "f_type": f_fundtypes[i],
            }
            try:
                seri.create(fund)
            except IntegrityError as e:
                pass


if __name__ == '__main__':
    """
    刷新后台管理数据
    """
    # get_stcak_codes()
    # get_fund_codes()
    update_stack_close20()
